package org.adoxx.pn.simulation.handlers.impl;

import java.util.HashMap;

import org.adoxx.pn.PetriNet;
import org.adoxx.pn.PetriNet.PL;
import org.adoxx.pn.PetriNet.TR;
import org.adoxx.pn.simulation.data.measures.Measures;
import org.adoxx.pn.simulation.handlers.SimulationListenerI;

public class ActivitiesTotCounter implements SimulationListenerI {

    HashMap<TR, Measures> activityList = new HashMap<TR, Measures>();
    
    @Override
    public void simulationStarted(PetriNet petriNet) throws Exception {
        
    }

    @Override
    public void transitionFiredEvent(TR transitionFired) throws Exception {
        
        if(transitionFired.additionalInfoList.containsKey("isEntryPoint")){
            Measures measures = activityList.get(transitionFired);
            if(measures==null){
                
                boolean hasCost = false;
                double cost = 0;
                for(PL place: transitionFired.previousList)
                    if(place.additionalInfoList.containsKey("cost")){
                        cost += Double.parseDouble(place.additionalInfoList.get("cost"));
                        hasCost = true;
                    }
                boolean hasExecutionTime = false;
                long executionTime = 0;
                for(PL place: transitionFired.previousList)
                    if(place.additionalInfoList.containsKey("executionTime")){
                        executionTime += Long.parseLong(place.additionalInfoList.get("executionTime"));
                        hasExecutionTime = true;
                    }
                
                if(hasCost || hasExecutionTime){
                    measures = new Measures();
                    measures.costs = cost;
                    measures.executionTime = executionTime;
                    activityList.put(transitionFired, measures);
                }
            } else {
                measures.numberOfExecutions++;
            }
        } else {
            //TODO: contare gli XOR o gli end ?
        }
    }

    @Override
    public void simulationEnded(PetriNet petriNet) throws Exception {
        
    }
    
    public HashMap<TR, Measures> getActivitiesMeasures(){
        return activityList;
    }

}
